package com.west.xu.provider.controller;

import com.google.gson.GsonBuilder;
import com.west.xu.provider.domain.CombinationNum;
import com.west.xu.provider.service.IProviderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@Api(value = "Tencent Provider")
public class ProviderController {
    private static final Logger LOG = LoggerFactory.getLogger(ProviderController.class);

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss:sss");

    @Autowired
    private IProviderService providerService;

    @RequestMapping(value = "/factorial/{number}", method = RequestMethod.GET)
    @ApiOperation(value = "数字阶乘计算服务", notes = "计算用户给定数字的阶乘")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "number", value = "待计算数字", dataType = "Long", required = true)
    })
    public Long factorial(@PathVariable(name = "number") Long number) {
        LOG.info("tencent-provider -- request param: [" + number.longValue() + "]");
        Long result = providerService.factorial(number);
        String resultStr = "request param: " + number.longValue() + ", response param: " + result + ",response time " + dateFormat.format(new Date());
        LOG.info("tencent-provider -- response info: [" + resultStr + "]");
        return result;
    }

    @RequestMapping(value = "/combination", method = RequestMethod.POST)
    @ApiOperation(value = "数字组合计算服务", notes = "计算用户给定数字的组合")
    public Long combination(@RequestBody CombinationNum combinationNum) {
        LOG.info("tencent-provider -- request param: [" + new GsonBuilder().create().toJson(combinationNum) + "]");
        Long result = providerService.combination(combinationNum);
        String resultStr = "request param: " + combinationNum.getDownNum() + "|" + combinationNum.getUpNum() + ", response param: " + result + ",response time " + dateFormat.format(new Date());
        LOG.info("tencent-provider -- response info: [" + resultStr + "]");
        return result;
    }
}
